import axios from "axios";
import * as constants from "./constants";

export const changeHomeData = (result) => ({
    type: constants.CHANGE_HOME_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    recommentList: result.recommentList
});

export const getHomeInfo = () => {
    return (dispatch) => {
        axios.get("/api/home.json").then((res) => {
            const result = res.data.data;
            const action = changeHomeData(result);
            dispatch(action);
        });
    }
};

export const changeArticleList = (result, page) => ({
    type: constants.CHANGE_ARTICLE_LIST,
    list: result,
    nextPage: page + 1
});

export const getMoreArticle = (page) => {
    return (dispatch) => {
        axios.get("/api/homeList.json?page=" + page).then((res) => {
            const result = res.data.data;
            const action = changeArticleList(result, page);
            dispatch(action);
        });
    }
};

export const toggleTopShow = (show) => ({
    type: constants.TOGGLE_TOP_SHOW,
    show: show
});